package com.volkanic.buttons;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;

/**
 * Created by Lucas on 22/07/2014.
 */
public class GenericButton {

    private MotionTextButton button;


    public static MotionTextButton CreateButton(ModelButton model) {
        GenericButton button = new GenericButton(model);
        return button.button;
    }

    /**
     * Constructor
     * @param model the data model of the button
     */
    private GenericButton(ModelButton model){
        float width = Gdx.graphics.getWidth() /2f;
        float height = Gdx.graphics.getHeight() / 10f;

        BitmapFont font = this.setUpFont(model.pathToFont, model.scaleFont);
        Skin skin = this.setUpSkin(model.pathToSkin);
        TextButtonStyle style = this.setUpButtonStyle(font, skin, model.styleUp, model.styleDown, model.styleOver);
        button = new MotionTextButton(model.text, style, model.currentPosition, model.lastPosition, model.speed, width);

        // Size and position
        button.setSize(width, height);

    }

    /**
     * Set up the textures of the button
     * @param internalPath the internal path to the skin
     * @return the skin of the button
     */
    private Skin setUpSkin(String internalPath){
        // Initialise button
        Skin skin = new Skin();
        TextureAtlas buttonAtlas = new TextureAtlas(Gdx.files.internal(internalPath));
        skin.addRegions(buttonAtlas);
        return skin;
    }

    /**
     * Set up the font use inside the textButton
     * @param scale the scale of the text style in the button
     * @return the new font used for the button
     */
    private BitmapFont setUpFont(String internalPath, float scale){
        BitmapFont font = new BitmapFont(Gdx.files.internal(internalPath), false);
        font.setScale(scale);
        return font;
    }

    /**
     * Set up the different texture for each state of the button
     * @param font the font used to create the style of the button
     * @return the new TextButtonStyle
     */
    private TextButtonStyle setUpButtonStyle(BitmapFont font, Skin skin, String up, String down, String over){
        TextButtonStyle buttonStyle = new TextButtonStyle();
        buttonStyle.up = skin.getDrawable(up);
        buttonStyle.down = skin.getDrawable(down);
        buttonStyle.over = skin.getDrawable(over);
        buttonStyle.font = font;
        return buttonStyle;
    }
}
